package roadsuggester.main;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import roadsuggester.db.UserLocationsDatabase;
import roadsuggester.db.UserLocationsDatabase.UserLocation;
import roadsuggester.testing.MockLocationProvider;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;


public class TrackerMap extends com.google.android.maps.MapActivity
{

	private MapView mapView;
	private UserLocation destination;
	private MapController mapController;
	private TrackerMapOverlay overlay;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		setContentView(R.layout.locatormap);
		mapView=(MapView) findViewById(R.id.mapview);
		mapController=mapView.getController();


		UserLocationsDatabase db=new UserLocationsDatabase(this);
		destination=db.getLocationByID(getIntent().getLongExtra("destinationID", -1));


		Location l=getLastKnownLocation();
		mapController.setCenter(Utils.locationToGeoPoint(l));
		mapController.setZoom(17);
		mapView.setBuiltInZoomControls(true);
		mapView.displayZoomControls(true);
		mapView.showContextMenu();

		overlay=new TrackerMapOverlay(getApplicationContext(), destination);
		List<Overlay> listOfOverlays=mapView.getOverlays();
		listOfOverlays.clear();
		listOfOverlays.add(overlay);

		mapView.invalidate();

		overlay.setLocation(l);


		registerLocationProvider();
		registerHeadingProvider();
	}


	private Location getLastKnownLocation()
	{
		LocationManager lm=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
		List<String> providers=lm.getProviders(true);

		Location l=null;

		for(int i=providers.size()-1; i>=0; i--)
		{
			l=lm.getLastKnownLocation(providers.get(i));
			if(l!=null)
				break;
		}
		return l;
	}


	private void registerLocationProvider()
	{
		LocationManager locationManager=(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
		LocationListener locationListener=new LocationListener()
		{
			public void onLocationChanged(Location location)
			{

				Utils.showToastText(TrackerMap.this, location.getLatitude()+", "+location.getLongitude());

				mapController.setCenter(Utils.locationToGeoPoint(location));
				overlay.setLocation(location);
			}

			public void onStatusChanged(String provider, int status, Bundle extras)
			{
			}

			public void onProviderEnabled(String provider)
			{
			}

			public void onProviderDisabled(String provider)
			{
			}
		};


		if(destination.label.equals("Home"))
		{
			//The hard-coded mock location provider for the home destination.
			String mockLocationProvider="CUSTOM_MOCK_LOCATION_PROVIDER"+Math.random();	//This is a hack.
			locationManager.addTestProvider(mockLocationProvider, false, false, false, false, true, true, true, 0, 5);
			locationManager.setTestProviderEnabled(mockLocationProvider, true);
			locationManager.requestLocationUpdates(mockLocationProvider, 0, 0, locationListener);

			try
			{
				InputStream dataInputStream=getAssets().open("home_threeblocks.gps");
				new MockLocationProvider(this, locationManager, mockLocationProvider, dataInputStream).start();
			}
			catch(IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}


		}
		else
		{

			locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
			locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
		}
	}

	private void registerHeadingProvider()
	{
		final SensorEventListener mListener=new SensorEventListener()
		{
			public void onSensorChanged(SensorEvent event)
			{
				overlay.setHeading(event.values[0]);
			}

			@Override
			public void onAccuracyChanged(Sensor arg0, int arg1)
			{

			}
		};
		SensorManager mSensorManager=(SensorManager) getSystemService(Context.SENSOR_SERVICE);
		Sensor mSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
		mSensorManager.registerListener(mListener, mSensor, SensorManager.SENSOR_DELAY_GAME);

	}

	protected boolean isRouteDisplayed()
	{
		return false;
	}

}
